package sz.szsmk.citizencard.ui.dialogs;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.TextView;

import sz.szsmk.citizencard.R;


public class PrivacyPolicyDlg {


    private static PrivacyPolicyDlg instance = new PrivacyPolicyDlg();

    private Activity activity;
    private Dialog dlg;


    private PrivacyPolicyDlg() {

    }

    public static PrivacyPolicyDlg getInstance(Activity activity) {
        instance.activity = activity;
        return instance;
    }

    public void buildDlg() {
        View view = LayoutInflater.from(activity).inflate(R.layout.dlg_privacy, null);

        TextView tvContent = (TextView) view.findViewById(R.id.tv_content);

//        SpannableString spannableString = new SpannableString(activity.getResources().getString(R.string.privacy_policy_tip_msg_1));
//        spannableString.setSpan(new LinkClickSpan(activity, "《服务协议》", new UrlSpanClickListener() {
//            @Override
//            public void onClick(View view, String content) {
//                Intent intent = new Intent(activity, BrowserActivity.class);
//                intent.putExtra(BrowserIntentKey.URL, Const.WebPageUrl.userProtocol());
//                activity.startActivity(intent);
//            }
//        }), 4, 10, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//        spannableString.setSpan(new LinkClickSpan(activity, "《隐私政策》", new UrlSpanClickListener() {
//            @Override
//            public void onClick(View view, String content) {
//                Intent intent = new Intent(activity, BrowserActivity.class);
//                intent.putExtra(BrowserIntentKey.URL, Const.WebPageUrl.privacyClause());
//                activity.startActivity(intent);
//            }
//        }), 11, 17, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//        tvContent.setText(spannableString);
        tvContent.setMovementMethod(LinkMovementMethod.getInstance());
        //解决TextView 添加ClickableSpan后点击选中文字背景变色问题
        tvContent.setHighlightColor(activity.getResources().getColor(android.R.color.transparent));


        TextView tvExit = (TextView) view.findViewById(R.id.tv_exit);
        TextView tvAgree = (TextView) view.findViewById(R.id.tv_agree);

        tvExit.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                if (onDisagreeButtonClickListener != null) {
                    onDisagreeButtonClickListener.onButtonClick();
                }
            }
        });

        tvAgree.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
                if (onAgreeButtonClickListener != null) {
                    onAgreeButtonClickListener.onButtonClick();
                }
            }
        });


        dlg = new Dialog(activity, R.style.DialogStyle);
        dlg.setContentView(view);

    }


    public void show() {
        dlg.show();
        dlg.setCancelable(false);

        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        WindowManager.LayoutParams lp = dlg.getWindow().getAttributes();
        lp.width = (int) (dm.widthPixels * 0.9); // 设置宽度
//        lp.height = (int) (dm.heightPixels * 0.9); // 设置宽度
//        lp.gravity = Gravity.CENTER;
        dlg.getWindow().setAttributes(lp);

//        dlg.setOnKeyListener(new DialogInterface.OnKeyListener() {
//            @Override
//            public boolean onKey(DialogInterface dialogInterface, int keyCode, KeyEvent keyEvent) {
//                if (keyCode == KeyEvent.KEYCODE_BACK) {
//                    activity.finish();
//                }
//                return false;
//            }
//        });
    }

    public void dismiss() {
        dlg.dismiss();
    }

    public interface OnDisagreeButtonClickListener {
        //确认按钮的点击事件接口
        void onButtonClick();
    }

    private OnDisagreeButtonClickListener onDisagreeButtonClickListener;

    public void setOnDisagreeButtonClickListener(OnDisagreeButtonClickListener onDisagreeButtonClickListener) {
        this.onDisagreeButtonClickListener = onDisagreeButtonClickListener;
    }

    public interface OnAgreeButtonClickListener {
        //确认按钮的点击事件接口
        void onButtonClick();
    }

    private OnAgreeButtonClickListener onAgreeButtonClickListener;

    public void setOnAgreeButtonClickListener(OnAgreeButtonClickListener onAgreeButtonClickListener) {
        this.onAgreeButtonClickListener = onAgreeButtonClickListener;
    }
}
